#include "convertFunctions.h"


unsigned char HexToAscii(unsigned char dt)
{
    dt &= 0x0F;                  //取输入的低半个字节
    if (dt >= 0x0A)
    {
        dt += 0x37;    //输出 ：0x41 ~ 0x46
    }
    else
    {
        dt |= 0x30;             //输出 ：0x30 ~ 0x39
    }
    return (dt);
}

//将字节数据转换为0x32格式的c语言中使用的格式
bool norMalFormatToCForMat(QByteArray *srcData, QByteArray *destData)
{
    int indexDest = 0;

    for(QByteArray::const_iterator srcItr = srcData->begin();\
                            srcItr != srcData->end(); srcItr++)
    {
        destData->insert(indexDest++, '0');
        destData->insert(indexDest++, 'x');
        destData->insert(indexDest++, HexToAscii(*srcItr>>4));
        destData->insert(indexDest++, HexToAscii(*srcItr));
        destData->insert(indexDest++, ',');
    }
    if(indexDest != 0)
    {
        indexDest--;
        destData->remove(indexDest,1);
    }
    return true;
}
//将字节数据转换为\x32格式的python语言中使用的格式
bool norMalFormatToPythonForMat(QByteArray *srcData, QByteArray *destData)
{
    int indexDest = 0;

    for(QByteArray::const_iterator srcItr = srcData->begin();\
                            srcItr != srcData->end(); srcItr++)
    {
        destData->insert(indexDest++, '\\');
        destData->insert(indexDest++, 'x');
        destData->insert(indexDest++, HexToAscii(*srcItr>>4));
        destData->insert(indexDest++, HexToAscii(*srcItr));
    }

    return true;
}

//ascii转换为hex
int ascii2Hex(QByteArray *inData, QByteArray *outData)
{
    QByteArray::const_iterator itr1;
    int outIndex = 0;
    itr1 = inData->begin();
    unsigned char tmpData_h = 0, tmpData_l = 0;
    unsigned char tmpData = 0;

    //for( itr1 = inData->begin(); itr1 != inData->end(); itr1++)
    for( outIndex = 0; outIndex != inData->size()/2; outIndex++)
    {
        if(itr1 != inData->end())
        {
            if(*itr1 <= '9' &&  *itr1 >= '0')
               tmpData_h = *itr1 - 0x30;
            else if(*itr1 <= 'F' &&  *itr1 >= 'A')
               tmpData_h = *itr1 - 'A' + 0xA;
            else if(*itr1 <= 'f' &&  *itr1 >= 'a')
               tmpData_h = *itr1 - 'a' + 0xA;
        }
        else
            return 0;

        itr1++;
        if(itr1 != inData->end())
        {
            if(*itr1 <= '9' &&  *itr1 >= '0')
               tmpData_l = *itr1 - 0x30;
            else if(*itr1 <= 'F' &&  *itr1 >= 'A')
               tmpData_l = *itr1 - 'A' + 0xA;
            else if(*itr1 <= 'f' &&  *itr1 >= 'a')
               tmpData_l = *itr1 - 'a' + 0xA;
        }
        else
            return 0;

        tmpData = (tmpData_h << 4) | tmpData_l;
        outData->insert(outIndex, tmpData);
        itr1++;
    }
    return 0;
}
//将（0x32）格式的c语言中使用的格式转换为字节数据
bool cForMatToNorMalFormat(QByteArray *srcData, QByteArray *destData)
{
    int indexDest = 0;
    bool findFlag = false;

    for(QByteArray::const_iterator srcItr = srcData->begin();\
                            srcItr != srcData->end(); srcItr++)
    {
        if(*srcItr == 'x')
        {
            findFlag = true;
            continue;
        }
        if(findFlag)
        {
            destData->insert(indexDest++, *srcItr);
            destData->insert(indexDest++, *(srcItr+1));
            findFlag = false;
        }
    }
    return true;
}
